package br.com.jogo.neg.actions;

import br.com.jogo.MagesGame;
import br.com.jogo.neg.Unit;

import com.badlogic.gdx.Gdx;

public class MoveAction implements UnitAction {
	
	public static final String KEY = "MOVE";

	public int x;
	public int y;

	public int velocity = 80;

	boolean isFinished = false;

	public MoveAction(float x, float y) {
		this.x = (int) x;
		this.y = (int) y;
	}

	@Override
	public void perform(Unit u, float gameTime) {

		int orientationX = 0;
		int orientationY = 0;

		if (u.bounds.x > x) {
			orientationX = -1;
		} else if (u.bounds.x < x) {
			orientationX = +1;
		}

		if (u.bounds.y > y) {
			orientationY = -1;
		} else if (u.bounds.y < y) {
			orientationY = 1;
		}

		if (orientationX == 0 && orientationY == 0) {
			isFinished = true;
		} else {

			float deltaX = gameTime * velocity * orientationX;
			float deltaY = gameTime * velocity * orientationY;

			float distanceX = u.bounds.x - x;
			float distanceY = u.bounds.y - y;

			if (Math.abs(distanceX) < Math.abs(deltaX)) {
				u.bounds.x = x;
			} else {
				u.bounds.x += deltaX;
			}

			if (Math.abs(distanceY) < Math.abs(deltaY)) {
				u.bounds.y = y;
			} else {
				u.bounds.y += deltaY;
			}
		}
	}

	@Override
	public boolean isFinished() {

		if (isFinished) {
			Gdx.app.log(MagesGame.LOG, "finished move");
		}

		return isFinished;
	}

	@Override
	public String getKey() {
		return KEY;
	}



}
